<?php
class Order
{
	public $_book;
	
	function __construct()
	{
		$this->_book = new Zend_Db_Table('order');
	}

	
	function getList()
	{
		$select = $this->_book->select()->from('order', 
    		array('created'=> new Zend_Db_Expr('date(created)'),
    		'arrival'=> new Zend_Db_Expr('date(arrival)'),
    		'departure'=> new Zend_Db_Expr('date(departure)'),
    		'Object', 'ID', 'status'
    		
    		))
		->order('status desc')->order('created desc'); 
	    $Rows = $this->_book->fetchAll($select);
		
		if ($Rows) return $Rows->toArray();
		return array();
	}
	
	
	function create($Params)
	{
	    Loader::includeModuleModels('catalog');
	    $Catalog = new Catalog();
	    
	    
	    $catalogObject = $Catalog->get($Params['basePlace']); 
	    
	    $Row = $this->_book->createRow();
	    
	    $Row->created = new Zend_Db_Expr("NOW()");
	    
	    $Row->arrival   = $Params['dateArrival'];
	    $Row->departure = $Params['dateDeparture'];
	    $Row->ObjectID    = $Params['basePlace'];
	    
	    $Row->ObjectID    = $Params['basePlace'];
	    $Row->Object      = $catalogObject['Name'];
	    
	    unset($Params['dateArrival']);
	    unset($Params['dateDeparture']);
	    unset($Params['basePlace']);
	    
	    $Row->Params = serialize($Params);
		$Row->save();
	}
	
	function get($id)
	{
		$Row = $this->_book->find($id)->current();
		$Item = $Row->toArray();
		$Item['Params'] = unserialize($Item['Params']);
		return $Item;  
	}
	
	
	function changeStatus($id, $newStatus)
	{
		$Row = $this->_book->find($id)->current();
		if (!$Row) throw new Exception('Object is not found');
		$Row->status = $newStatus;
		$Row->save();
	}

	function cancel($id)
	{
		$Row = $this->_book->find($id)->current();
		if ($Row) $Row->delete();
	}

    function removeAll()
	{
	    $this->_book->delete("1");
	}
	
	
	
}